<?php
include('../config.php');
header("Access-Control-Allow-Origin:*");
$phone = $_POST['phone'];
$goods_id = $_POST['goods_id'];
$goods_count = $_POST['goods_count'];
$car_size = $_POST['car_size'];


$sql = "select goods_count from shop_car where phone= '$phone' and goods_id=$goods_id";

// $res时一个资源类型
$res = mysql_query($sql);
$count=mysql_num_rows($res);

$list = array();
// 通过循环去资源里抓取数据，每取一行，都是一个$row，取完了循环也就结束了
  while ($row = mysql_fetch_assoc($res)) {
    array_push($list, $row);
  }
if ($count !=0) {
    $newcount=$list[0][goods_count]+$goods_count;
    $sqlupd = "update shop_car set goods_count=$newcount where phone='$phone' and goods_id=$goods_id ";
    //       update shop_car set goods_count=4 where phone='15181158388' and goods_id=18;
    //绝对字符串好像不行
    // $res时一个资源类型
    $resupd = mysql_query($sqlupd);    
    
    if ($resupd) {
        echo json_encode(array(
          "code" => 200,
          "body" => array(
            "msg" => '购物车更新成功'
          )
        ));
      } else {
        echo json_encode(array(
          "code" => 201,
          "body" => array(
            "msg" => '网络错误，请重试',
            "res"=> $resupd
          )
        ));
      }

} else {      
    $sqladd = "insert into shop_car (phone,goods_id,goods_count,car_size) values ('$phone',$goods_id,$goods_count,'$car_size')";

    $resadd = mysql_query($sqladd);

    if ($resadd) {
      echo json_encode(array(
        "code" => 200,
        "body" => array(
          "msg" => "购物车新增成功！",
          "res"=> $resadd
        )
      ));
    } else {
      echo json_encode(array(
        "code" => 202,
        "body" => array(
          "msg" => "网络错误，别试了",
          "res"=> $resadd
        )
      ));
    }
}




?>

